cap log close
log using "$logs/an-iat.log", replace

use "$data/iat", clear
keep if k12==1

unique countyid

// Condition on having all covariates
egen rm = rowmiss(implicitbias amind eastasian southasian nathaw black blackwhite multiother otherun female agecat ed schoolyear)
keep if rm == 0
drop rm

unique countyid

// Merge with sample
merge m:1 countyid using "$data/commonsamp", keep(1 3)

	egen tag = tag(countyid)
	tab _merge if tag // 72%
	keep if _merge==3
	drop _merge tag
	
// Merge with covariates
merge m:1 countyid using "$data/seda_covariates_clean", assert(2 3) keep(3) nogen


est clear

// Models...

eststo: mixed implicitbias ///
	i.schoolyear || fips: ||countyid: 
	estat icc
	estadd scalar icc_county= r(icc2)
	estadd scalar icc_state= r(icc3)
	mat define n=e(N_g)
	estadd scalar county_n=n[1,2]
	
	esttab using "$output/an-iat.csv", compress nogaps se replace title("Multilevel Models Predicting IAT Scores, Educators only (2009-2016)") ///
		note("Note: All models include random intercepts for county nested within state and year FE.") ///
		scalar(icc_county icc_state county_n) ///
		transform(ln*: exp(@) exp(@)) ///
		eqlabels("" "sd(state)" "sd(county)" "sd(residual)", none) 
	   
eststo: mixed implicitbias ///
	amind eastasian southasian nathaw black blackwhite multiother otherun ///
	female i.agecat i.ed ///
	i.schoolyear || fips: ||countyid: 
	estat icc
	estadd scalar icc_county= r(icc2)
	estadd scalar icc_state= r(icc3)
	mat define n=e(N_g)
	estadd scalar county_n=n[1,2]
	
	esttab using "$output/an-iat.csv", compress nogaps se replace title("Multilevel Models Predicting IAT Scores, Educators only (2009-2016)") ///
		note("Note: All models include random intercepts for county nested within state and year FE.") ///
		scalar(icc_county icc_state county_n) ///
		transform(ln*: exp(@) exp(@)) ///
		eqlabels("" "sd(state)" "sd(county)" "sd(residual)", none) 
		
eststo: mixed implicitbias ///
	sesall ///
	i.schoolyear || fips: ||countyid: 
	estat icc
	estadd scalar icc_county= r(icc2)
	estadd scalar icc_state= r(icc3)
	mat define n=e(N_g)
	estadd scalar county_n=n[1,2]
	
	esttab using "$output/an-iat.csv", compress nogaps se replace title("Multilevel Models Predicting IAT Scores, Educators only (2009-2016)") ///
		note("Note: All models include random intercepts for county nested within state and year FE.") ///
		scalar(icc_county icc_state county_n) ///
		transform(ln*: exp(@) exp(@)) ///
		eqlabels("" "sd(state)" "sd(county)" "sd(residual)", none) 		
		
eststo: mixed implicitbias ///
	perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
	i.schoolyear || fips: ||countyid: 
	estat icc
	estadd scalar icc_county= r(icc2)
	estadd scalar icc_state= r(icc3)
	mat define n=e(N_g)
	estadd scalar county_n=n[1,2]
	
	esttab using "$output/an-iat.csv", compress nogaps se replace title("Multilevel Models Predicting IAT Scores, Educators only (2009-2016)") ///
		note("Note: All models include random intercepts for county nested within state and year FE.") ///
		scalar(icc_county icc_state county_n) ///
		transform(ln*: exp(@) exp(@)) ///
		eqlabels("" "sd(state)" "sd(county)" "sd(residual)", none) 				

eststo: mixed implicitbias ///
	ppexp_inst stutch_all ///
	i.schoolyear || fips: ||countyid: 
	estat icc
	estadd scalar icc_county= r(icc2)
	estadd scalar icc_state= r(icc3)
	mat define n=e(N_g)
	estadd scalar county_n=n[1,2]
	
	esttab using "$output/an-iat.csv", compress nogaps se replace title("Multilevel Models Predicting IAT Scores, Educators only (2009-2016)") ///
		note("Note: All models include random intercepts for county nested within state and year FE.") ///
		scalar(icc_county icc_state county_n) ///
		transform(ln*: exp(@) exp(@)) ///
		eqlabels("" "sd(state)" "sd(county)" "sd(residual)", none) 						
		
eststo: mixed implicitbias ///
	flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
	i.schoolyear || fips: ||countyid: 
	estat icc
	estadd scalar icc_county= r(icc2)
	estadd scalar icc_state= r(icc3)
	mat define n=e(N_g)
	estadd scalar county_n=n[1,2]
	
	esttab using "$output/an-iat.csv", compress nogaps se replace title("Multilevel Models Predicting IAT Scores, Educators only (2009-2016)") ///
		note("Note: All models include random intercepts for county nested within state and year FE.") ///
		scalar(icc_county icc_state county_n) ///
		transform(ln*: exp(@) exp(@)) ///
		eqlabels("" "sd(state)" "sd(county)" "sd(residual)", none) 						

eststo: mixed implicitbias ///
	sesall ///
	perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
	ppexp_inst stutch_all ///
	flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
	i.schoolyear || fips: ||countyid: 
	estat icc
	estadd scalar icc_county= r(icc2)
	estadd scalar icc_state= r(icc3)
	mat define n=e(N_g)
	estadd scalar county_n=n[1,2]
	
	esttab using "$output/an-iat.csv", compress nogaps se replace title("Multilevel Models Predicting IAT Scores, Educators only (2009-2016)") ///
		note("Note: All models include random intercepts for county nested within state and year FE.") ///
		scalar(icc_county icc_state county_n) ///
		transform(ln*: exp(@) exp(@)) ///
		eqlabels("" "sd(state)" "sd(county)" "sd(residual)", none) 								
		
eststo: mixed implicitbias ///
	amind eastasian southasian nathaw black blackwhite multiother otherun ///
	female i.agecat i.ed ///
	sesall ///
	perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
	ppexp_inst stutch_all ///
	flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
	i.schoolyear || fips: ||countyid: 
	estat icc
	estadd scalar icc_county= r(icc2)
	estadd scalar icc_state= r(icc3)
	mat define n=e(N_g)
	estadd scalar county_n=n[1,2]
	
	esttab using "$output/an-iat.csv", compress nogaps se replace title("Multilevel Models Predicting IAT Scores, Educators only (2009-2016)") ///
		note("Note: All models include random intercepts for county nested within state and year FE.") ///
		scalar(icc_county icc_state county_n) ///
		transform(ln*: exp(@) exp(@)) ///
		eqlabels("" "sd(state)" "sd(county)" "sd(residual)", none) 		
		
log close
